import React, { Component } from "react";
import { SafeAreaView, Text, View, StyleSheet, TouchableOpacity, Image, TextInput, PermissionsAndroid, Platform, DeviceEventEmitter } from 'react-native';
import Constant from '../../utils/Constant'//全局变量
import { getImgUrl } from '../../utils/ComFunction';
import HttpUtils from "../../utils/HttpUtils";
import HttpUrls from "../../utils/HttpUrls";
import Header from "../../components/header";
import { ToastToUser } from "../../utils/CustomToast";
import ActionSheet from 'react-native-actionsheet'
import ImageCropPicker from 'react-native-image-crop-picker';
import Upload from '../../components/upload/Upload'
import { requestCameraPermission, requestWritePermission } from '../../utils/Permission';
import Loading from "../../utils/Loading";
import AndroidModule from "../../utils/AndroidConnent";
import PermissionDialog from "../../components/dialog/PermissionDialog";
const isIos = Platform.OS == 'ios';

/**
 * 通用修改页面
 */
export default class ModifyInputPage extends Component {

    constructor(props) {
        super(props);
        this.state = {
            title: this.props.route.params.title,
            value: this.props.route.params.value,
            type: this.props.route.params.type ? this.props.route.params.type : '',//1修改发货人信息
            name: this.props.route.params.name ? this.props.route.params.name : '',
            phone: this.props.route.params.phone ? this.props.route.params.phone : ''
        };
    }

    componentDidMount() {

    }

    render() {
        const navigation = this.props.navigation;
        return (
            <View
                style={styles.container}>
                <Header navigation={navigation} Title={this.state.title} hideSepLine={true} />

                <TextInput
                    style={styles.inputStyle}
                    value={this.state.value}
                    placeholder="请输入"
                    onChangeText={(text) => this.setState({ value: text })}>

                </TextInput>

                {this.state.type == '1' ?

                    <TextInput
                        style={styles.inputStyle}
                        value={this.state.name}
                        placeholder="请输入姓名"
                        onChangeText={(text) => this.setState({ name: text })}>

                    </TextInput>
                    : null
                }


                {this.state.type == '1' ?
                    <TextInput
                        style={styles.inputStyle}
                        value={this.state.phone}
                        placeholder="请输入电话号码"
                        inputMode="tel"
                        onChangeText={(text) => this.setState({ phone: text })}>

                    </TextInput>
                    :
                    null
                }


                <TouchableOpacity
                    style={styles.btnConfirm}
                    onPress={() => {
                        if (this.props.route.params.onConfirm) {
                            if (this.state.type == '1') {
                                this.props.route.params.onConfirm(this.state.value, this.state.name, this.state.phone)
                            } else {
                                this.props.route.params.onConfirm(this.state.value)
                            }

                            this.props.navigation.goBack()
                        }
                    }}>
                    <Text style={{ color: '#333333', fontSize: 14 }}>确认修改</Text>
                </TouchableOpacity>

            </View>
        );
    }

    updateUserSubmit() {

        if (this.state.name.length == 0) {
            ToastToUser('请输入用户名')
            return
        }

        let params = {
            userId: Constant.userInfo.userId,
            fullname: this.state.name
        }

        HttpUtils.postRequest(HttpUrls.updateUser, params)
            .then((result) => {
                let code = result.code
                if (code == 0) {
                    AndroidModule.updateUserInfo({ fullname: this.state.name })
                    DeviceEventEmitter.emit('commonEvent', { type: 'updateUserInfo', fullname: this.state.name })
                    this.props.navigation.goBack()
                }
            })
            .catch((error) => {

            });
    }

};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#f9f9f9',
    },


    inputStyle: {

        width: '100%',
        backgroundColor: '#fff',
        padding: 10,
        fontSize: 14,
        color: '#333333'
    },

    btnConfirm: {
        height: 44,
        width: '100%',
        backgroundColor: '#fff',
        justifyContent: 'center',
        alignItems: 'center',
        marginTop: 15
    }

});